//
//  HomeViewController.m
//  Futures
//
//  Created by Cheng on 2017/6/20.
//  Copyright © 2017年 Cheng. All rights reserved.
//

#import "HomeViewController.h"
#import "RecommendCycleView.h"
#import "HomeViewModel.h"
#import "ButtonMenu.h"
#import "ChartViewController.h"
#import "QuoteModel.h"
#import "FuturesCell.h"
#import "TransacionViewModel.h"

#define kCycleViewH 160
#define kButtonMenuH 114
#define kFuturesCellID @"kFuturesCellID"
#define kGetQuoteListID @"kGetQuoteListID"

@interface HomeViewController () <UITableViewDelegate,UITableViewDataSource>{
    NSArray *_quoteList;
}
//  轮播图
@property (strong,nonatomic) RecommendCycleView *recommendCycleView;
//  按钮列表
@property (strong,nonatomic) ButtonMenu *buttonMenu;
//  表格
@property (weak, nonatomic) IBOutlet UITableView *tableView;



@end

@implementation HomeViewController

- (RecommendCycleView *)recommendCycleView
{
    if (!_recommendCycleView){
        _recommendCycleView = [RecommendCycleView recommendCycleView];
        _recommendCycleView.frame = CGRectMake(0, -kCycleViewH-kButtonMenuH, SCREEN_WITDTH, kCycleViewH);
    }
    return _recommendCycleView;
}

- (ButtonMenu *)buttonMenu{
    if (!_buttonMenu) {
        _buttonMenu = [ButtonMenu buttonMenu];
        _buttonMenu.frame = CGRectMake(0, -kButtonMenuH, SCREEN_WITDTH, kButtonMenuH);
    }
    return _buttonMenu;
}
- (IBAction)simulatedTrading:(id)sender {
    [self performSegueWithIdentifier:NSStringFromClass([ChartViewController class]) sender:@"富时A50"];
}
- (IBAction)supervisionQualification:(id)sender {
}
- (IBAction)riskWarning:(id)sender {
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initalization];
    
    [self.tableView addSubview:self.recommendCycleView];
    
    [self.tableView addSubview:self.buttonMenu];
    
    self.tableView.contentInset = UIEdgeInsetsMake(kCycleViewH+kButtonMenuH, 0, 0, 0);

    [self.tableView registerNib:[UINib nibWithNibName:@"FuturesCell" bundle:nil] forCellReuseIdentifier:kFuturesCellID];
    
    self.tableView.bounces = NO;
    //  禁用延时(按钮点击不产生动画效果)
    self.tableView.delaysContentTouches = NO;
    
    [self refreshData];
}

- (void)initalization{
    _quoteList = [NSArray array];
    
    self.title = @"首页";
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTitle:@"客服" icon:@"customerservice" target:self action:@selector(clickCustomerService)];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTitle:@"新手" icon:@"novice" target:self action:@selector(clickNovice)];
}

- (void)clickCustomerService{
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel:13692227717"]]];
    [self.view addSubview:callWebview];
}

- (void)clickNovice{

}

- (void)refreshData{
    // 请求轮播数据
    [HomeViewModel requestCycleData:^(NSArray *cycleArray) {
        [self.recommendCycleView reloadDataWithModelArray:cycleArray];
    }];
    // 请求合约报价
    [TransacionViewModel requestSymbolsWithBlock:^(id obj) {
        _quoteList = obj;
        [self.tableView reloadData];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _quoteList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FuturesCell *cell = nil;
    
    cell = [tableView dequeueReusableCellWithIdentifier:kFuturesCellID forIndexPath:indexPath];
    
    QuoteModel *model = _quoteList[indexPath.row];
    
    [cell setCellWithModel:model];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    MYLog(@"%ld",(long)indexPath.row);
    QuoteModel *model = _quoteList[indexPath.row];
    [Global shareInstance].quoteModel = model;
    [self performSegueWithIdentifier:NSStringFromClass([ChartViewController class]) sender:model.Symbolname];
    
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:NSStringFromClass([ChartViewController class])]) {
        ChartViewController *chartVc = segue.destinationViewController;
        chartVc.title = sender;
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self timingEvent];
    
    __weak typeof(self) weakSelf = self;
    [[JX_GCDTimerManager sharedInstance] scheduledDispatchTimerWithName:kGetQuoteListID timeInterval:5.0f queue:nil repeats:YES actionOption:AbandonPreviousAction action:^{
        
        [weakSelf timingEvent];
    }];
}


// 请求合约报价
- (void)timingEvent{
    [TransacionViewModel requestSymbolsWithBlock:^(id obj) {
        _quoteList = obj;
        [self.tableView reloadData];
    }];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    if ([[JX_GCDTimerManager sharedInstance]existTimer:kGetQuoteListID]) {
        [[JX_GCDTimerManager sharedInstance] cancelTimerWithName:kGetQuoteListID];
    }
}

@end
